package com.tuoren.forward.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tuoren.forward.constant.CommonConstant;
import com.tuoren.forward.entity.Dictionary;
import com.tuoren.forward.entity.Hospital;
import com.tuoren.forward.entity.dto.HospitalDto;
import com.tuoren.forward.entity.req.HospitalSearchReq;
import com.tuoren.forward.mapper.DictionaryMapper;
import com.tuoren.forward.mapper.HospitalMapper;
import com.tuoren.forward.util.Result;
import com.tuoren.forward.util.ResultData;
import com.tuoren.forward.util.ResultPage;
import com.tuoren.forward.util.UUIDUtil;

@Service
public class HospitalService {
	
	@Autowired
	HospitalMapper hospitalMapper;
	
	@Autowired
	RestTemplate restTemplate;
	
	@Autowired
	DictionaryMapper dictionaryMapper;
	
	@Autowired
	UserService userService;
	
	/**
	 * @title 查询医院
	 * @param req
	 * @return
	 */
	public ResultPage<Hospital> search(HospitalSearchReq req) {
		Page<Object> page = PageHelper.startPage(req.getPage(), req.getSize());
		HospitalDto dto = new HospitalDto();
		BeanUtils.copyProperties(req, dto);
		List<Hospital> hospitals = hospitalMapper.select(dto);
		return ResultPage.success(hospitals, page.getTotal());
	}
	
	/**
	 * @title 添加医院
	 * @param req
	 * @return
	 */
	@Transactional
	public Result add(Hospital req) {
		Date date = new Date();
		Dictionary dic = dictionaryMapper.selectByPrimaryKey("hospitalCode");
		String maxCode = dic.getValue();
		int code = Integer.parseInt(maxCode);
		code++;
		if(code>9999) {
			return Result.fail("医院编码超出范围");
		}
		String curCode = String.format("%04d", code);
		Hospital record = hospitalMapper.selectByCode(curCode);
		if(record != null) {
			return Result.fail("医院编码已存在");
		}
		req.setId(UUIDUtil.get32UUID());
		req.setCode(curCode);
		req.setCreatetime(date);
		hospitalMapper.insertSelective(req);
		
		dic.setValue(curCode);
		dictionaryMapper.updateByPrimaryKey(dic);
//		UserAddReq userReq = new UserAddReq();
//		userReq.setUsername(curCode);
//		List<String> roleIds = new ArrayList<String>();
//		roleIds.add(CommonConstant.ROLE_USER); //添加用户角色
//		userReq.setRoleIds(roleIds);
//		Result result = userService.add(userReq);
//		return result;
		return Result.success();
	}
	
	/**
	 * @title 添加医院
	 * @param req
	 * @return ResultData<Hospital>
	 */
	@Transactional
	public ResultData<Hospital> addBack(Hospital req) {
		Date date = new Date();
		Dictionary dic = dictionaryMapper.selectByPrimaryKey("hospitalCode");
		String maxCode = dic.getValue();
		int code = Integer.parseInt(maxCode);
		code++;
		if(code>9999) {
			return ResultData.fail(null,"医院编码超出范围");
		}
		String curCode = String.format("%04d", code);
		Hospital record = hospitalMapper.selectByCode(curCode);
		if(record != null) {
			return ResultData.fail(null,"医院编码已存在");
		}
		Hospital hos = hospitalMapper.selectByPrimaryKey(req.getId());
		if(hos!=null) {
			return ResultData.failNull("医院已存在");
		}
//		req.setId(UUIDUtil.get32UUID());
		req.setCode(curCode);
		req.setCreatetime(date);
		hospitalMapper.insertSelective(req);
		
		dic.setValue(curCode);
		dictionaryMapper.updateByPrimaryKey(dic);
//		UserAddReq userReq = new UserAddReq();
//		userReq.setUsername(curCode);
//		List<String> roleIds = new ArrayList<String>();
//		roleIds.add(CommonConstant.ROLE_USER); //添加用户角色
//		userReq.setRoleIds(roleIds);
//		userService.add(userReq);
		return ResultData.success(req);
	}
	
	/**
	 * @title 修改医院
	 * @param req
	 * @return
	 */
	public Result edit(Hospital req) {
		Date date = new Date();
		if(StringUtils.isEmpty(req.getId())) {
			return Result.fail(CommonConstant.LACK_PARAM);
		}
		req.setCode(null); //编码不可修改
		req.setModifytime(date);
		hospitalMapper.updateByPrimaryKeySelective(req);
		return Result.success();
	}
	
	/**
	 * @title 删除医院
	 * @param id
	 * @return
	 */
	public Result delete(String id) {
		hospitalMapper.deleteByPrimaryKey(id);
		return Result.success();
	}
	
	public String localtion() {
		HttpHeaders headers = new HttpHeaders();
	    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
	    
	    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	    map.add("mcc", "460");
	    map.add("mnc", "0");
	    map.add("lac","21133");
	    map.add("ci","52365");
	    map.add("hex","10");
	    
	    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
	    
		String result = restTemplate.postForObject("http://bs.openluat.com/get_gps", request, String.class);
		System.out.println("result========="+result);
		JSONObject json = JSONObject.parseObject(result);
		if(json.containsKey("result")) {
			return json.getString("result");
		}
		return "";
	}

}
